<template>
  <div class="login-container">
    <div class="login-wrapper">
      <!-- 登录头部 -->
      <div class="login-header">
        <h1 class="login-title">用户登录</h1>
        <p class="login-subtitle">欢迎回到皮影艺术平台</p>
      </div>
      
      <!-- 登录表单 -->
      <form @submit.prevent="handleLogin" class="login-form">
        <!-- 账号/手机号输入框 -->
        <div class="form-group">
          <label for="account" class="form-label">账号/手机号</label>
          <input 
            type="text" 
            id="account" 
            v-model="loginForm.account"
            class="form-input" 
            placeholder="请输入账号或手机号"
            :class="{ 'focused': focusedInput === 'account', 'error': errors.account }"
            @focus="focusedInput = 'account'"
            @blur="focusedInput = ''"
            autocomplete="username"
          >
          <span v-if="errors.account" class="error-message">{{ errors.account }}</span>
        </div>
        
        <!-- 密码输入框 -->
        <div class="form-group">
          <div class="label-wrapper">
            <label for="password" class="form-label">密码</label>
            <router-link to="/forgot-password" class="forgot-password">忘记密码？</router-link>
          </div>
          <div class="password-input-wrapper">
            <input 
              :type="showPassword ? 'text' : 'password'" 
              id="password" 
              v-model="loginForm.password"
              class="form-input" 
              placeholder="请输入密码"
              :class="{ 'focused': focusedInput === 'password', 'error': errors.password }"
              @focus="focusedInput = 'password'"
              @blur="focusedInput = ''"
              autocomplete="current-password"
            >
            <button 
              type="button" 
              class="toggle-password" 
              @click="showPassword = !showPassword"
            >
              {{ showPassword ? '隐藏' : '显示' }}
            </button>
          </div>
          <span v-if="errors.password" class="error-message">{{ errors.password }}</span>
        </div>
        
        <!-- 记住密码选项 -->
        <div class="form-group remember-me">
          <label class="remember-me-label">
            <input type="checkbox" v-model="loginForm.rememberMe">
            <span>记住密码</span>
          </label>
        </div>
        
        <!-- 登录按钮 -->
        <button 
          type="submit" 
          class="login-btn"
          :disabled="isSubmitting"
        >
          <span v-if="isSubmitting" class="loading-spinner"></span>
          <span v-else>登录</span>
        </button>
        
        <!-- 注册链接 -->
        <div class="register-link">
          还没有账号？<router-link to="/register">立即注册</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 登录表单数据
const loginForm = reactive({
  account: '',
  password: '',
  rememberMe: false
})

// 表单状态
const isSubmitting = ref(false)
const showPassword = ref(false)
const focusedInput = ref('')
const errors = reactive({})

// 表单验证
const validateForm = () => {
  // 重置错误信息
  Object.keys(errors).forEach(key => delete errors[key])
  let isValid = true
  
  // 验证账号
  if (!loginForm.account.trim()) {
    errors.account = '请输入账号或手机号'
    isValid = false
  } else if (loginForm.account.length < 2) {
    errors.account = '账号格式不正确'
    isValid = false
  }
  
  // 验证密码
  if (!loginForm.password) {
    errors.password = '请输入密码'
    isValid = false
  } else if (loginForm.password.length < 6) {
    errors.password = '密码长度不能少于6位'
    isValid = false
  }
  
  return isValid
}

// 处理登录
const handleLogin = async () => {
  // 验证表单
  if (!validateForm()) {
    return
  }
  
  // 设置提交状态
  isSubmitting.value = true
  
  try {
    // 模拟登录API调用
    await new Promise(resolve => setTimeout(resolve, 1500))
    
    // 模拟登录成功，创建用户信息
    const userInfo = {
      username: loginForm.account.includes('@') || loginForm.account.length === 11 ? 
               '皮影爱好者' : loginForm.account,
      level: '普通会员',
      phone: loginForm.account.length === 11 ? loginForm.account : ''
    }
    
    // 保存登录状态和用户信息
    localStorage.setItem('isLoggedIn', 'true')
    localStorage.setItem('userInfo', JSON.stringify(userInfo))
    
    // 如果勾选记住密码，保存账号信息
    if (loginForm.rememberMe) {
      localStorage.setItem('rememberedAccount', loginForm.account)
    } else {
      localStorage.removeItem('rememberedAccount')
    }
    
    // 跳转到首页
    router.push('/home')
  } catch (error) {
    console.error('登录失败:', error)
    alert('登录失败，请检查账号和密码是否正确')
  } finally {
    isSubmitting.value = false
  }
}

// 页面加载时，尝试读取记住的账号
const rememberedAccount = localStorage.getItem('rememberedAccount')
if (rememberedAccount) {
  loginForm.account = rememberedAccount
  loginForm.rememberMe = true
}
</script>

<style scoped>
/* 登录容器 */
.login-container {
  max-width: 600px;
  margin: 0 auto;
  padding: 40px 20px;
  font-family: 'Microsoft YaHei', sans-serif;
}

.login-wrapper {
  width: 100%;
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  padding: 30px;
}

/* 登录头部 */
.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-title {
  font-size: 28px;
  font-weight: bold;
  color: var(--color-primary);
  margin-bottom: 8px;
  font-family: 'STZhongsong', 'SimSun', serif;
  letter-spacing: 2px;
}

.login-subtitle {
  font-size: 16px;
  color: var(--color-text-medium);
  font-family: 'STKaiti', 'KaiTi', serif;
}

/* 表单样式 */
.login-form {
  width: 100%;
}

.form-group {
  margin-bottom: 20px;
}

.form-label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 500;
  color: var(--color-text-dark);
}

.label-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.forgot-password {
  font-size: 12px;
  color: var(--color-primary);
  text-decoration: none;
  transition: color 0.3s ease;
}

.forgot-password:hover {
  text-decoration: underline;
}

.form-input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s ease;
  background-color: white;
}

.form-input:focus,
.form-input.focused {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(157, 41, 51, 0.1);
}

.form-input.error {
  border-color: #e74c3c;
}

.error-message {
  display: block;
  margin-top: 5px;
  font-size: 12px;
  color: #e74c3c;
}

/* 密码输入框 */
.password-input-wrapper {
  position: relative;
}

.toggle-password {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--color-text-medium);
  font-size: 12px;
  cursor: pointer;
  padding: 5px 10px;
  transition: color 0.3s ease;
}

.toggle-password:hover {
  color: var(--color-primary);
}

/* 记住密码选项 */
.remember-me {
  margin-bottom: 24px;
}

.remember-me-label {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.remember-me-label input[type="checkbox"] {
  margin-right: 8px;
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.remember-me-label span {
  font-size: 14px;
  color: var(--color-text-dark);
}

/* 登录按钮 */
.login-btn {
  width: 100%;
  padding: 12px;
  background-color: var(--color-primary);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-btn:hover:not(:disabled) {
  background-color: #8a232b;
}

.login-btn:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

/* 加载动画 */
.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid transparent;
  border-top: 2px solid currentColor;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-right: 8px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 注册链接 */
.register-link {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: var(--color-text-medium);
}

.register-link a {
  color: var(--color-primary);
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
}

.register-link a:hover {
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    padding: 20px 15px;
  }
  
  .login-wrapper {
    padding: 20px;
  }
  
  .login-title {
    font-size: 24px;
  }
  
  .form-input {
    padding: 14px 16px;
    font-size: 16px;
  }
}
</style>